#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
'''
Created on 2016年5月16日
@author: zhaoxiang
根据ugc_type或者url解析出用户浏览内容(id)，
并根据uid，guid，匹配出dora_id
用于hive,用法：
select TRANSFORM(user_id, unique_id, channel,  ugc_type, ugc_content, path,platform,logdate) USING 'python extract_track_action.py' as (doraid,user_id,parsed_type,parsed_content,logdate)
'''

import sys,os
import re
from urlparse import urlparse
import urllib
sys.path.append('/home/hadoop/udf_data/python/user_track')
os.environ['PYTHON_EGG_CACHE'] = '/tmp/hadoop/.python-eggs/'
os.environ['PYTHON_EGG_DIR']='/tmp/hadoop/.python-eggs/'
from UrlParser import UrlParser
from MongoClient import MongoClient
import gua
from util_func import strdecode

reload(sys)
sys.setdefaultencoding("utf8")

mongo_db = MongoClient(gua.conn_mongo["host"], gua.conn_mongo["db"])
uid_doraid = {}
guid_doraid = {}
deviceid_doraid = {}
urlparser = UrlParser()

########################################################################################
#web,mobile日志处理
#rowSet=[user_id, unique_id, channel, ugc_type, ugc_content, path,platform, logdate]
########################################################################################
def extract_web_action(rowSet) :
    if len(rowSet) < 8 : 
        return
    dora_id = 0
    #根据uid或者guid从mongodb解析dora_id
    uid = rowSet[0]
    guid = rowSet[1]
    if uid.isdigit() : 
        uid = int(uid)
    else : 
        uid = 0
    if uid :
        if uid in uid_doraid : 
            dora_id = uid_doraid[uid]
        else :
            uid_rowSet = mongo_db.execute("uid_doraid_1", {"_id":uid}, "selectone")
            if uid_rowSet : 
                dora_id = uid_rowSet["dora_id"]
                uid_doraid[uid] = dora_id
    if len(guid) == 36 :
        if dora_id == 0 :
            if guid in guid_doraid : 
                dora_id = guid_doraid[guid]
            else :
                guid_rowSet = mongo_db.execute("guid_doraid_1", {"_id":guid}, "selectone")
                if guid_rowSet : 
                    dora_id = guid_rowSet["dora_id"]
                    guid_doraid[guid] = dora_id
                    if uid : 
                        uid_doraid[uid] = dora_id
        else : 
            guid_doraid[guid] = dora_id
    #没登录过的，在web和m端的，过滤掉(app端有设备ID，所以不过滤)        
    if dora_id <= 0 : 
        return 
    #解析ugc类型和内容(id)
    #channel= rowSet[2]
    ugc_type = rowSet[3]
    ugc_content = rowSet[4]
    path = "http://" + rowSet[5]
    
    parsed_type=None
    parsed_content=None
    if ugc_type == "bbs_thread" or ugc_type == "mbbs_thread" :
        if ugc_content.isdigit() : 
            tid = int(ugc_content)
            parsed_type = "v_tbbs"
            parsed_content = tid
    elif ugc_type == "ask_question" or ugc_type == "mask_question" :
        if ugc_content.isdigit() :
            qid = int(ugc_content)
            parsed_type = "v_ask"
            parsed_content = qid
    elif ugc_type == "plan_trip" or ugc_type == "mplan_trip" :
        if ugc_content.isdigit() :
            plan_id = int(ugc_content)
            parsed_type = "v_plan"
            parsed_content = plan_id
    elif ugc_type == "plan_edit" or ugc_type == "mplan_edit" :
        if ugc_content.isdigit() :
            plan_id = int(ugc_content)
            parsed_type = "edit_plan"
            parsed_content = plan_id
    elif ugc_type == "place_country" or ugc_type == "mplace_country" :
        if ugc_content.isdigit() :
            country_id = int(ugc_content)
            parsed_type = "v_place"
            parsed_content = {"country":country_id}
    elif ugc_type == "place_city" or ugc_type == "mplace_city" :
        if ugc_content.isdigit() :
            city_id = int(ugc_content)
            parsed_type = "v_place"
            parsed_content = {"city":city_id}
    elif ugc_type == "place_poi" or ugc_type == "mplace_poi" :
        if ugc_content.isdigit() :
            poi_id = int(ugc_content)
            parsed_type = "v_place"
            parsed_content = {"poi":poi_id}
    elif ugc_type == "place_mguide" or ugc_type == "mplace_mguide" :
        if ugc_content.isdigit() :
            mguide_id = int(ugc_content)
            parsed_type = "v_mguide"
            parsed_content = mguide_id
    elif ugc_type == "guide_detail" or ugc_type == "mguide_detail" :
        if ugc_content.isdigit() :
            guide_id = int(ugc_content)
            parsed_type = "v_guide"
            parsed_content = guide_id
    elif ugc_type == "z_detail" or ugc_type == "mz_detail" :
        if ugc_content.isdigit() :
            lm_id = int(ugc_content)
            parsed_type = "v_lm"
            parsed_content = lm_id
    elif ugc_type == "hotel_city" :
        if ugc_content.isdigit() :
            city_id = int(ugc_content)
            parsed_type = "v_hotellist"
            parsed_content = {"city":city_id}
    elif ugc_type == "hotel_country" :
        if ugc_content.isdigit() :
            country_id = int(ugc_content)
            parsed_type = "v_hotellist"
            parsed_content = {"country":country_id}
    elif ugc_type == "hotel_detail" :
        if ugc_content.isdigit() :
            hotel_id = int(ugc_content)
            parsed_type = "v_hotel"
            parsed_content = hotel_id
    elif ugc_type == "goto_hotel" :
        if ugc_content.isdigit() :
            hotel_id = int(ugc_content)
            parsed_type = "v_goto_hotel"
            parsed_content = hotel_id
    else:
        #根据ugc_type无法判断，解析url
        url = strdecode(urllib.unquote(urllib.unquote(path)))
        parsedUrl = urlparse(url)        
        if parsedUrl.netloc == "appview.qyer.com" :
            if parsedUrl.path.startswith("/bbs") :
                tid = 0
                m = re.search(r"thread-(\d+)-", parsedUrl.path)
                if m : 
                    tid = int(m.group(1))
                else :
                    m = re.search(r"tid=(\d+)", parsedUrl.query)
                    if m : tid = int(m.group(1))
                if tid : 
                    parsed_type = "v_tbbs"
                    parsed_content = tid
        elif parsedUrl.netloc == "plan.qyer.com" or (parsedUrl.netloc == "appview.qyer.com" and parsedUrl.path.startswith("/plan")) :
            if "/calendar" in parsedUrl.path or "/cost" in parsedUrl.path or "/message" in parsedUrl.path or "/map" in parsedUrl.path or "/traffic" in parsedUrl.path or "/hotel" in parsedUrl.path :
                m = re.search(r"/(\d+)/", parsedUrl.path)
                if m : 
                    plan_id = int(m.group(1))
                    parsed_type = "v_plan"
                    parsed_content = plan_id
            else :
                m = re.search(r"tripapp/(\d+)", parsedUrl.path)
                if m :
                    plan_id = int(m.group(1))
                    parsed_type = "v_plan"
                    parsed_content = plan_id
                else :
                    m = re.search(r"poi_(\d+)", parsedUrl.path)
                    if m :
                        poi_id = int(m.group(1))
                        parsed_type = "v_place"
                        parsed_content = {"poi":poi_id}
        else :
            parse_rowSet = urlparser.parse_url(path)
            if parse_rowSet["action_type"] == "view_guide_detail" : 
                if parse_rowSet["action_content"]["guide_id"] : 
                    parsed_type = "v_guide"
                    parsed_content = parse_rowSet["action_content"]["guide_id"]
            elif parse_rowSet["action_type"] == "view_lm_list" :
                country_id = parse_rowSet["action_content"]["country_id"]
                city_id = parse_rowSet["action_content"]["city_id"]
                parsed_type = "v_lmlist"
                parsed_content={}
                if country_id :
                    parsed_type = "v_lmlist"
                    parsed_content["country"] = country_id
                if city_id : 
                    parsed_type = "v_lmlist"
                    parsed_content["city"] = city_id
            elif parse_rowSet["action_type"].startswith("search") :
                parsed_type = "search"
                parsed_content={}
                wd=parse_rowSet["action_content"].get("wd", "");
                wd_code=parse_rowSet["action_content"].get("wd_code", "")
                parsed_content["wd"] = [wd] if wd else []
                parsed_content["wd_code"] = [wd_code] if wd_code else []
                parsed_content["cityids"] = parse_rowSet["action_content"].get("cityids", [])
                parsed_content["countryids"] = parse_rowSet["action_content"].get("countryids", [])
            elif parse_rowSet["action_type"] == "goto_hotel" :
                if "hotel_id" in parse_rowSet["action_content"] : 
                    parsed_type = "v_goto_hotel"
                    parsed_content=  parse_rowSet["action_content"]["hotel_id"]

    if parsed_type and parsed_content:
        print '%s\t%s\t%s\t%s\t%s' % (dora_id, uid, parsed_type, parsed_content,rowSet[7])


########################################################################################
#app日志处理
#rowSet=[user_id, unique_id, channel, ugc_type, ugc_content, path,platform, logdate]
########################################################################################
def extract_app_action(rowSet) :
    if len(rowSet) < 8 : 
        return
    dora_id = 0
    uid = rowSet[0]
    device_id = rowSet[1]
    if uid.isdigit() : 
        uid = int(uid)
    else : 
        uid = 0
        
    if uid :
        if uid in uid_doraid :
            dora_id = uid_doraid[uid]
        else :
            uid_result = mongo_db.execute("uid_doraid_1", {"_id":uid}, "selectone")
            if uid_result : 
                dora_id = uid_result["dora_id"]
                uid_doraid[uid] = dora_id
        
    shrink_device_id = set(device_id)
    #过滤00000，00-000-0000， unknow
    if len(shrink_device_id) == 1 or (len(shrink_device_id) == 2 and "-" in shrink_device_id) or device_id.lower().startswith("unknow") : 
        return
    if dora_id == 0 :
        if device_id in deviceid_doraid : 
            dora_id = deviceid_doraid[device_id]
        else :
            device_result = mongo_db.execute("device_doraid_1", {"_id":device_id}, "selectone")
            if device_result : 
                dora_id = device_result["dora_id"]
                deviceid_doraid[device_id] = dora_id
                if uid :
                    uid_doraid[uid] = dora_id
    if dora_id == 0 : 
        return
    channel = rowSet[2]
    ugc_type = rowSet[3]
    ugc_content = rowSet[4]
    path = rowSet[5]
    
    parsed_type=None
    parsed_content=None
    if channel == "place" :
        if ugc_type == "country_detail" :
            if ugc_content.isdigit() :
                country_id = int(ugc_content)
                parsed_type = "v_place"
                parsed_content = {"country":country_id} 
        elif ugc_type == "city_detail" :
            if ugc_content.isdigit() : 
                city_id = int(ugc_content)
                parsed_type = "v_place"
                parsed_content = {"city":city_id}
        elif ugc_type == "poi_detail" :
            if ugc_content.isdigit() : 
                poi_id = int(ugc_content)
                parsed_type = "v_place"
                parsed_content = {"poi":poi_id}                
        elif ugc_type == "poi" :
            if ugc_content.isdigit() : 
                poi_id = int(ugc_content)
                parsed_type = "v_place"
                parsed_content = {"poi":poi_id}                
        elif ugc_type == "poi_category" :
            if ugc_content.isdigit() : 
                city_id = int(ugc_content)
                parsed_type = "v_place"
                parsed_content = {"city":city_id} 
        elif ugc_type == "search" : 
            parsed_type = "search"
            parsed_content={}
            parsed_content["wd"] = [ugc_content] 
    elif channel == "search" and ugc_type == "index" : 
        parsed_type = "search"
        parsed_content={}
        parsed_content["wd"] = [ugc_content]
    elif channel == "bbs" :
        if ugc_type == "search" : 
            parsed_type = "search"
            parsed_content={}
            parsed_content["wd"] = [ugc_content]            
        elif ugc_type == "thread" :
            if ugc_content.isdigit() : 
                tid = int(ugc_content)
                parsed_type = "v_tbbs"
                parsed_content = tid                
    elif channel == "guide" :
        if ugc_type == "detail" :
            if ugc_content.isdigit() : 
                guide_id = int(ugc_content)
                parsed_type = "v_guide"
                parsed_content = guide_id                
        elif ugc_type == "country" :
            if ugc_content.isdigit() : 
                c_id = int(ugc_content)
                parsed_type = "v_glist"
                parsed_content={"country":c_id}
        elif ugc_type == "city" :
            if ugc_content.isdigit() : 
                c_id = int(ugc_content)
                parsed_type = "v_glist"
                parsed_content={"city":c_id}
    elif channel == "mguide" :
        if ugc_type == "detail" :
            if ugc_content.isdigit() : 
                guide_id = int(ugc_content)
                parsed_type = "v_mguide"
                parsed_content=guide_id
    elif channel == "z" :
        if ugc_type == "detail" :
            if ugc_content.isdigit() : 
                lm_id = int(ugc_content)
                parsed_type = "v_lm"
                parsed_content = lm_id                
        elif ugc_type == "choose" :
            parse_result = urlparser.parse_url("http://" + path)
            if parse_result["action_type"] == "view_lm_list" :
                country_id = parse_result["action_content"]["country_id"]
                city_id = parse_result["action_content"]["city_id"]
                parsed_type = "v_lmlist"
                parsed_content = {}  
                if country_id : 
                    parsed_content["country"] = country_id
                if city_id : 
                    parsed_content["city"] = city_id
        elif ugc_type == "add_favor" :
            if ugc_content.isdigit() : 
                parsed_type = "collect_lm"
                parsed_content = int(ugc_content) 
    elif channel == "hotel" :
        if ugc_type == "list" :
            if ugc_content.isdigit() : 
                city_id = int(ugc_content)
                parsed_type = "v_hotellist"
                parsed_content = {"city":city_id}

    if parsed_type and parsed_content:
        print '%s\t%s\t%s\t%s\t%s' % (dora_id, uid, parsed_type, parsed_content,rowSet[7])

if __name__ == '__main__':
    for line in sys.stdin:
        rowSet = line.strip().split('\t')
        if (len(rowSet) < 8):
            continue
        if rowSet[6]=="app":
            extract_app_action(rowSet)
        elif rowSet[6]=="web" or rowSet[6]=="mobile":
            extract_web_action(rowSet)
    mongo_db.close()
